//
//  SpaceshipView.m
//  SpaceshipWar
//
//  Created by adripop on 09/05/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "SpaceshipView.h"
#import "myShipWarThread.h"

@implementation SpaceshipView

-(void)awakeFromNib {
	//[theSpaceshipView setDelegate:self]
}
- (BOOL)acceptsFirstResponder { 
	return YES;
}

- (id)initWithFrame:(NSRect)frame {
	self = [super initWithFrame:frame];
	
	xPosition = (int)( frame.size.width / 2 );
	yPosition = (int) frame.size.height - 50;
	if (self) {
		//backgroundColor = [NSColor redColor];
		//backgroundColor = [[NSColor colorWithPatternImage:[NSImage imageNamed:@"background"]] copy];
		//[[NSColor colorwithPatternImage:[NSImage imageNamed:@"background"]] set];
		backgroundColor = [NSColor blackColor];
		[NSBezierPath fillRect:[self bounds]];
				
		[self setNeedsDisplay:YES];
	}
	
	
	myThreadInst = [myShipWarThread alloc];
	NSThread* myThread = [[NSThread alloc] initWithTarget:myThreadInst
                                       selector:@selector(myThreadMainMethod:)
                                       object:nil];
	[myThread start];
	return self;
}

// draw meth
- (void)drawRect:(NSRect)rect {
	// fill the background
	[backgroundColor set];
	NSRectFill([self bounds]);
	
	// create 
	rectangle = NSMakeRect(xPosition, yPosition, 20 , 20);
	[[NSColor colorWithCalibratedHue:0.20 
						  saturation:1
						  brightness:1 
							   alpha:1] set];
	NSRectFill(rectangle);
	
	// spaceship drawing
	
	spaceship = [NSBezierPath bezierPath];
	[spaceship	setLineWidth:1];
	
	NSPoint leftBottom = {10, 120};
	NSPoint rightBottom = {70,120};
	NSPoint topCenter = {40, 40};
	NSPoint middleCenter = {40, 100};
	
	
	[spaceship	moveToPoint:leftBottom];
	[spaceship	lineToPoint:middleCenter];
	
	[spaceship	lineToPoint:rightBottom];
	//[path moveToPoint:topCenter];
	/*
	 [path curveToPoint:rightBottom 
		 controlPoint1:NSMakePoint (40, 40) 
		 controlPoint2:NSMakePoint (50, 40)];
	 */
	[spaceship	lineToPoint:topCenter];
	
	//[path moveToPoint:topCenter];
	/*
	 [path curveToPoint:leftBottom
		 controlPoint1:NSMakePoint (26, 40)
		 controlPoint2:NSMakePoint (30, 40)];
	*/
	
	[spaceship	lineToPoint:leftBottom];

	[[NSColor colorWithCalibratedHue:0.48
						  saturation:1
						  brightness:1
							   alpha:1] set];
	[spaceship	fill];
	[[NSColor blackColor] set];

	[spaceship	stroke];
}

// keyboard handling

- (void)keyDown:(NSEvent *)event {
	//NSLog(@"delegate debug!");
	NSString *keys = [event charactersIgnoringModifiers];
	
	if(keys && [keys length] >0) {
		int c = [keys characterAtIndex:0];
		if (c == NSLeftArrowFunctionKey) {
			// move it left
			//[theSpaceshipView MoveCube];
			xPosition -= 5;
		} else if (c == NSRightArrowFunctionKey) {
			// move it right
			//[theSpaceshipView MoveCube];
			xPosition += 5;
		} else if (c == NSUpArrowFunctionKey) {
			yPosition -= 5;
		} else if (c == NSDownArrowFunctionKey) {
			yPosition += 5;
		}
		[myThreadInst keyDownKey:c];
	}
	[self setNeedsDisplay:YES];
}


- (void)MoveCube {
	
	xPosition += (int) ((random() % 100) + 1);
	yPosition += (int) ((random() % 100) + 1);
	[self setNeedsDisplay:YES];
}

- (void)MoveSpaceship {
	[self setNeedsDisplay:YES];
}

- (BOOL)isFlipped
{
	return YES;
}
@end
